/*
 * @Author: szx
 * @Date: 2022-02-24 12:46:57
 * @LastEditTime: 2022-02-24 12:59:51
 * @Description:
 * @FilePath: \leetcode\100-199\142\142_2.js
 */

function ListNode(val) {
    this.val = val;
    this.next = null;
}

/**
 * @param {ListNode} head
 * @return {ListNode}
 */
 var detectCycle = function (head) {
    if (!head || !head.next) return null;
    let slow = head;
    let fast = head;
    while (fast && fast.next) {
        fast = fast.next.next;
        slow = slow.next;
        if(fast == slow ) break;
    }
    if(fast!=slow) return null;
    let cur = head;
    while(cur!=slow){
        slow =slow.next;
        cur = cur.next;
    }
    return cur;
};
